//
//  MovieViewController.m
//  HuiWenMovie
//
//  Created by ios on 15/7/19.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieCell.h"
#import "UIViewExt.h"
#import "PosterView.h"
@interface MovieViewController ()
{

    BOOL isLeft;
    NSMutableArray *modelArray;
    NSMutableArray *imageArray;
}

@end

@implementation MovieViewController
#pragma mark init
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        self.title = @"电影";
    
    }
    
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.edgesForExtendedLayout = UIRectEdgeNone;
    [self loadData];
    [self configureRightItem];
    [self createMovieTable];
    [self createPoster];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
  //  self.navigationController.navigationBar.translucent = YES;
    // Do any additional setup after loading the view.
}
#pragma mark createView
- (void)configureRightItem
{
    UIView *rightBarItem = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    UIButton *rightBarItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBarItemButton setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home.png"] forState:UIControlStateNormal];
    [rightBarItemButton setImage:[UIImage imageNamed:@"list_home.png"] forState:UIControlStateNormal];
    [rightBarItemButton sizeToFit];
    rightBarItemButton.tag =1;
    
    UIButton *rightBarItemButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBarItemButton2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home.png"] forState:UIControlStateNormal];
    [rightBarItemButton2 setImage:[UIImage imageNamed:@"poster_home.png"] forState:UIControlStateNormal];
    [rightBarItemButton2 sizeToFit];
    rightBarItemButton2.tag = 2;
    rightBarItemButton2.hidden = YES;
    
    [rightBarItemButton addTarget:self action:@selector(flipButton:) forControlEvents:UIControlEventTouchUpInside];
    [rightBarItemButton2 addTarget:self action:@selector(flipButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [rightBarItem addSubview:rightBarItemButton];
    [rightBarItem addSubview:rightBarItemButton2];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarItem];
    
    // rightBarItemButton2.hidden = YES;
}

- (void)createMovieTable
{
    movieList = [[UITableView alloc]initWithFrame:self.view.frame];
    movieList.dataSource = self;
    movieList.delegate = self;
    movieList.hidden = YES;
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    [movieList registerNib:nib forCellReuseIdentifier:@"cell"];
    [self.view addSubview:movieList];
    

}

- (void)createPoster
{
    poster = [[PosterView alloc]initWithFrame:self.view.frame];
    poster.backgroundColor = [UIColor greenColor];
    poster.models = modelArray;
    [self.view addSubview:poster];

}
#pragma mark configure table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [modelArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = modelArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 90;
}
#pragma mark buttonAction

- (void)flipButton:(UIButton *)button
{
    UIView *view = self.navigationItem.rightBarButtonItem.customView;
    
    UIButton *btn1 = (UIButton *)[view viewWithTag:1];
    UIButton *btn2 = (UIButton *)[view viewWithTag:2];
    
    isLeft = !isLeft;
    [self flipView:view isLeft:isLeft];
    [self flipView:self.view isLeft:isLeft];
    
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    
    movieList.hidden = !movieList.hidden;
    poster.hidden = !poster.hidden;

}
#pragma mark flipView
- (void)flipView:(UIView *)view isLeft:(BOOL)isleft
{
    UIViewAnimationOptions option = isleft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    
    [UIView transitionWithView:view duration:0.3 options:option animations:nil completion:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)loadData
{
    modelArray = [[NSMutableArray alloc]init];
    imageArray = [[NSMutableArray alloc] init];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"us_box.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *subjects = [content valueForKey:@"subjects"];
    
    for (NSDictionary* dic in subjects) {
        NSDictionary *subject = [dic valueForKey:@"subject"];
        MovieModel *model = [[MovieModel alloc]init];
        model.title = [subject valueForKey:@"title"];
        model.year = [subject valueForKey:@"year"];
        model.englishTitle = [subject valueForKey:@"original_title"];
        NSNumber *average = [[subject valueForKey:@"rating"] valueForKey:@"average"];
        model.average = [average floatValue];
        model.images = [subject valueForKey:@"images"];
    
        [modelArray addObject:model];
        [imageArray addObject:[model.images valueForKey:@"large"]];
        
    }
    

}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    
    return UIStatusBarStyleLightContent;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
